<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="机型编号" prop="modeCode">
        <el-input v-model="queryParams.modeCode" placeholder="请输入机型编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="客户" prop="customerId">
        <el-select v-model="queryParams.customerId" placeholder="请选择客户" clearable size="small">
          <el-option v-for="customer in customerList" :key="customer.id" :label="customer.customerName"
                     :value="customer.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="生产仓" prop="productionWarehouseId">
        <el-select v-model="queryParams.productionWarehouseId" placeholder="请选择所在生产仓" clearable size="small">
          <el-option v-for="warehouse in productionWarehouseList" :key="warehouse.id" :label="warehouse.name"
                     :value="warehouse.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="三方仓" prop="tripartiteWarehouseId">
        <el-select v-model="queryParams.tripartiteWarehouseId" placeholder="请选择所在三方仓" clearable size="small">
          <el-option v-for="warehouse in tripartiteWarehouseList" :key="warehouse.id" :label="warehouse.name"
                     :value="warehouse.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否落地检" prop="landingQc">
        <el-select v-model="queryParams.landingQc" placeholder="请选择是否落地检" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.LANDING_QC)"
                     :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="客户零件号" prop="partNumber">
          <el-input v-model="queryParams.partNumber" placeholder="请输入客户零件号" clearable
                    @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="零件简号" prop="partNumberShort">
        <el-input v-model="queryParams.partNumberShort" placeholder="请输入零件简号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否直发" prop="isDirect">
        <el-select v-model="queryParams.isDirect" placeholder="请选择是否直发客户" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.IS_DIRECT)"
                     :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作工具栏 -->
    <div class="toolbarBox">
      <div class="left-toolbar">
        <el-button type="primary" icon="el-icon-plus" size="medium" @click="openForm(undefined)"
                   v-hasPermi="['hs:material-mode:create']">新增
        </el-button>
        <el-button type="warning" icon="el-icon-download" size="medium" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['hs:material-mode:export']">导出
        </el-button>
        <div class="toolsbtn">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </div>
      </div>
      <div class="right-toolbar">
        <!-- 分页组件 -->
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
          layout="prev, pager, next"
          :background="false"
        />
      </div>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column type="index" label="序号" align="center" width="50"/>
      <el-table-column label="机型编号" align="center" prop="modeCode"/>
      <el-table-column label="客户名称" align="center" prop="customerName"/>
      <el-table-column label="生产仓" align="center" prop="productionWarehouseName"/>
      <el-table-column label="三方仓" align="center" prop="tripartiteWarehouseName"/>
      <el-table-column label="落地检" align="center" prop="landingQc">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.LANDING_QC" :value="scope.row.landingQc"/>
        </template>
      </el-table-column>
      <el-table-column label="客户零件号" align="center" prop="partNumber"/>
      <el-table-column label="零件简号" align="center" prop="partNumberShort"/>
      <el-table-column label="物料箱容量" align="center" prop="boxCapacityCount"/>
      <el-table-column label="直发客户" align="center" prop="isDirect">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.IS_DIRECT" :value="scope.row.isDirect"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
                     v-hasPermi="['hs:material-mode:update']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['hs:material-mode:delete']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框(添加 / 修改) -->
    <MaterialModeForm ref="formRef" @success="getList"/>
  </div>
</template>

<script>
import * as MaterialModeApi from '@/api/hs/materialmode';
import MaterialModeForm from './MaterialModeForm.vue';
import {DICT_TYPE} from "@/utils/dict";
import {getWarehouseSimpleList} from "@/api/hs/warehouse";
import {getCustomerSimpleList} from "@/api/hs/customer";

export default {
  name: "MaterialMode",
  computed: {
    DICT_TYPE() {
      return DICT_TYPE
    }
  },
  components: {
    MaterialModeForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机型信息列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        modeCode: null,
        customerId: null,
        customerName: null,
        productionWarehouseId: null,
        productionWarehouseName: null,
        tripartiteWarehouseId: null,
        tripartiteWarehouseName: null,
        landingQc: null,
        createTime: [],
        partNumber: null,
        partNumberShort: null,
        isDirect: null,
      },
      productionWarehouseList: [],
      tripartiteWarehouseList: [],
      customerList: [],
    };
  },
  created() {
    this.getList();
    this.getProductionWarehouseList();
    this.getTripartiteWarehouseList();
    this.getCustomerList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await MaterialModeApi.getMaterialModePage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除机型信息编号为"' + id + '"的数据项?')
      try {
        await MaterialModeApi.deleteMaterialMode(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有机型信息数据项?');
      try {
        this.exportLoading = true;
        const data = await MaterialModeApi.exportMaterialModeExcel(this.queryParams);
        this.$download.excel(data, '机型信息.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
    /** 查询仓库列表 */
    async getProductionWarehouseList() {
      let query = {
        warehouseType: 'PRODUCTION'
      }
      getWarehouseSimpleList(query).then(res => {
        this.productionWarehouseList = res.data;
      })
    },
    /** 查询仓库列表 */
    async getTripartiteWarehouseList() {
      let query = {
        warehouseType: 'TRIPARTITE'
      }
      getWarehouseSimpleList(query).then(res => {
        this.tripartiteWarehouseList = res.data;
      })
    },
    /** 查询客户列表 */
    async getCustomerList() {
      getCustomerSimpleList().then(res => {
        this.customerList = res.data;
      })
    },
  }
};
</script>
